/* File makes Table 11 and coefficients for Figure 6 */

/* 	Input files: 	fw_new_follows, permno_symbol_match_final, fw_rolling_newsfeed, user_experience, crsp_returns, crsp_calcs_temp
	Output files: T11_Returns.tex, Ret_sentiment_xsd_1_15_gasvi.csv, Ret_sentiment_xsd_16_30_gasvi.csv */

**********************************************
net install lincomestadd, from("https://github.com/benzipperer/lincomestadd/raw/master")
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************



use fw_new_follows, clear

foreach var of varlist _all {
	rename `var' `=lower("`var'")'   // "
}
replace bull=0 if mi(bull)
replace bear=0 if mi(bear)
drop net*


merge m:1 symbolid using permno_symbol_match_final, keep(match) nogen 

drop symbol_ticker tsymbol symbol_title comnam  

merge 1:1 date userid symbolid using fw_rolling_newsfeed, keep(match) nogen  
	label var l30_bull "N bullish impressions about symbolid user saw over past 30days"
	label var l30_bear "N bearish impressions about symbolid user saw over past 30days"
	label var t30_bull "N bullish impressions about symbolid on STwits over past 30days"
	label var t30_bear "N bullish impressions about symbolid on STwits over past 30days"

merge m:1 userid using user_experience, keep(master match) nogen  
drop active experience

gen bull_xNov=bull*novice
gen bull_xPro=bull*pro
gen bull_xInter=bull*intermediate

compress
order date userid symbolid permno 
save temp_main_trades, replace


*****************************************************************************************************


use crsp_returns, clear  

merge 1:m permno date using temp_main_trades, keep(match) nogen

gen int year=year(date)
gen byte month=month(date)
g int ym=ym(year,month)
format ym %tm


merge m:1 permno date using crsp_calcs_temp, keep(master match) nogen 

cap n drop meanret60d sdret60d
drop mktdate ret vwretd logturn logturn_l1 meanlogturn_140_20 ablogvol ablogvol_l1 abret sdabret_l1to5 month
order permno date year  ym userid symbolid bull bear 
save regs_onreturns, replace


cap n erase temp_main_trades.dta



use asvi_l5day symbolid date using stock_day, clear   

merge 1:m symbolid date using regs_onreturns, keep(match using) nogen  

gen asvi5_abret5=asvi_l5day * cabret_l1to5 

drop if (l30_bull + l30_bear)<5  

save temp_regs_onreturns, replace


gen mean_l30 = (l30_bull-l30_bear) / (l30_bull+l30_bear)  
gen sd_l30   = ((l30_bull*(1-mean_l30)^2 + l30_bear*(-1-mean_l30)^2 ) / (l30_bull+l30_bear) )^0.5


scalar extra_controls = "cabret_l1to5 cabret_l6to30 asvi_l5day asvi5_abret5"  

keepvar userid symbolid permno ym date bull sd_l30 xret_5day_f2 xret_10day_f2 xret_15day_f2 bull_xNov bull_xPro bull_xInter `=extra_controls', asis 

scalar clvars = "permno userid"
scalar FE ="i.date i.userid#i.symbolid i.symbolid#i.ym"  


scalar days = 5  

eststo r`=days'd: reghdfe xret_`=days'day_f2 bull  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

eststo r`=days'd_sd: reghdfe xret_`=days'day_f2 1.bull##c.sd_l30  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

eststo r`=days'd_exp: reghdfe xret_`=days'day_f2 bull bull_xNov bull_xInter bull_xPro  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)
	test bull_xNov= bull_xPro  
	test bull_xNov= bull_xInter
	test bull_xPro= bull_xInter

eststo r`=days'd_sd_exp: reghdfe xret_`=days'day_f2 1.bull##c.sd_l30 bull_xNov bull_xInter bull_xPro  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)


scalar days = 10  

eststo r`=days'd: reghdfe xret_`=days'day_f2 bull  `=extra_controls', absorb(`=FE') vce(cl `=clvars')   
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

eststo r`=days'd_sd: reghdfe xret_`=days'day_f2 1.bull##c.sd_l30  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)

eststo r`=days'd_exp: reghdfe xret_`=days'day_f2 bull bull_xNov bull_xInter bull_xPro  `=extra_controls', absorb(`=FE') vce(cl `=clvars') 
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)
	test bull_xNov= bull_xPro
	test bull_xNov= bull_xInter
	test bull_xPro= bull_xInter

eststo r`=days'd_sd_exp: reghdfe xret_`=days'day_f2 1.bull##c.sd_l30 bull_xNov bull_xInter bull_xPro  `=extra_controls', absorb(`=FE') vce(cl `=clvars')  
	qui sum `e(depvar)' if e(sample)==1
	qui estadd scalar avg = r(mean)


* -------------------- *

cap estadd local Day_FE "Y" : *
cap estadd local user_sy_FE "Y" : *
cap estadd local symbol_monthFE "Y" : *

cd "`=tabledir'"
#delimit ; 

local stats_opts " stats(N N_clust1 N_clust2 r2 Day_FE user_sy_FE symbol_monthFE, fmt(%18.0fc %18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("\# obs." "\# clusters (stock)" "\# clusters (users)" "$\textit{R}^2$" "Day FE" "User $\times$ stock FE" "Stock $\times$ month FE") ) " ;

local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles(
"Baseline" 
"\shortstack{$+$ \\ sd(rec. signals)}"
"\shortstack{Baseline with \\ experience}"
"\shortstack{$+$ \\ sd(rec. signals)}"
"Baseline" 
"\shortstack{$+$ \\ sd(rec. signals)}" 
"\shortstack{Baseline with \\ experience}" 
"\shortstack{$+$ \\ sd(rec. signals)}"          )
mgroups("Dep. var.: abnormal return\textsubscript{i,s,(t+2 $\rightarrow$ t+6)}" "Dep. var.: abnormal return\textsubscript{i,s,(t+2 $\rightarrow$ t+11)}"  
, pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "T11_Returns" ;

esttab r5d r5d_sd r5d_exp r5d_sd_exp r10d r10d_sd r10d_exp r10d_sd_exp  using `filename'.tex, `opts' `stats_opts' 
rename (bull 1.bull) drop(_cons) order(1.bull#c.sd_l30 1.bull sd_l30 bull_xNov bull_xInter bull_xPro)  
coef(
1.bull#c.sd_l30 "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t} $\times$ sd(rec. signals)\textsubscript{i,s,t}"
1.bull "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" 
sd_l30 "sd(received signals)\textsubscript{i,s,t}" 
bull_xNov "$\mathbbm{1}$ Bull\textsubscript{i,s,t} $\times$ $\mathbbm{1}$ Novice investor\textsubscript{i}" 
bull_xInter "$\mathbbm{1}$ Bull\textsubscript{i,s,t} $\times$ $\mathbbm{1}$ Intermediate investor\textsubscript{i}" 
bull_xPro "$\mathbbm{1}$ Bull\textsubscript{i,s,t} $\times$ $\mathbbm{1}$ Professional investor\textsubscript{i}" 
cabret_l1to5 "Cum. ab. returns\textsubscript{s,(t-5 to t-1)}"
cabret_l6to30 "Cum. ab. returns\textsubscript{s,(t-30 to t-6)}"
asvi_l5day "log GoogleASVI\textsubscript{s,(t-5 to t-1)}"
asvi5_abret5 "(log GoogleASVI $\times$ Cum. ab. ret.)\textsubscript{s,(t-5 to t-1)}" 
) replace  ;
#delimit cr 
cd "`=maindir'" 


 


* ------------------------------------------------------------------- *
* ------------------------------------------------------------------- *
* ------------------------------------------------------------------- *
* ------------------------------------------------------------------- *
* ------------------------------------------------------------------- *


/* Daily return regressions used for Figure 6 in the paper */


use temp_regs_onreturns, clear

drop if (l30_bull + l30_bear)<5  

gen mean_l30= (l30_bull-l30_bear) / (l30_bull+l30_bear)  
gen sd_l30 = ((l30_bull*(1-mean_l30)^2 + l30_bear*(-1-mean_l30)^2 ) / (l30_bull+l30_bear) )^0.5

keep xret_f* bull date ym userid symbolid permno sd_l* cabret_l1to5 cabret_l6to30 asvi_l5day asvi5_abret5


scalar FE ="i.date i.userid#i.symbolid i.symbolid#i.ym"  
scalar clvars = "i.permno i.userid"

cd "`=tabledir'"
scalar lhsvar = "xret_f"
forval k=2/31 {
replace xret_f`k' = xret_f`k'*100
eststo i`k': reghdfe `=lhsvar'`k' 1.bull##c.sd_l30 cabret_l1to5 cabret_l6to30 asvi_l5day asvi5_abret5, absorb(`=FE') vce(cl `=clvars')

	qui lincomestadd 1.bull+1.bull#c.sd_l30, statname (net_)

drop `=lhsvar'`k'
}

* ------------------------- *

cap estadd local D_FE "Y" : *
cap estadd local U_Sy_FE "Y" : *
cap estadd local S_MonthFE "Y" : *

cd "`=tabledir'"
#delimit ; 

local stats_opts " stats(net_b net_se N N_clust1 N_clust2 r2 D_FE U_Sy_FE S_MonthFE, fmt(%18.2fc %18.2fc %18.0fc %18.0fc %18.0fc %12.2fc %12.2fc %12.0fc %12.0fc) labels("Net effect" "s.e. of net effect" "\# obs." "\# clusters (permno $\times$ day)" "\# clusters (users)" $\textit{R}^2$ "Day FE" "User $\times$ symbol FE" "Symbol $\times$ month FE") ) " ;

* Table days 1-15 
local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+1" "t+2" "t+3" "t+4" "t+5" "t+6" "t+7" "t+8" "t+9" "t+10" "t+11" "t+12" "t+13" "t+14" "t+15"        )
mgroups("Dependent variable: abnormal return\textsubscript{\textbf{s}} in percentage points on day: "  
, pattern(1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "Ret_sentiment_xsd_1_15_gasvi" ;

esttab i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 using `filename'.tex, `opts' `stats_opts' 
drop(_cons) coef(
1.bull "Declared Bull\textsubscript{s,t}" 
sd_l30 "S.d. received impressions(30days)" 
1.bull#c.sd_l30 "Bull\textsubscript{s,t} $\times$ s.d. rec. impressions " 
cabret_l1to5 "Cum. ab. returns\textsubscript{s,(t-5 to t-1)}"
cabret_l6to30 "Cum. ab. returns\textsubscript{s,(t-30 to t-6)}"
asvi_l5day "Log GoogleASVI\textsubscript{s,(t-5 to t-1)}"
asvi5_abret5 "(Log GoogleASVI $\times$ Cum. ab. ret.)\textsubscript{s,(t-5 to t-1)}" 
) replace  ;

esttab i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 using `filename'.csv, se nostar stats(N N_clust1 N_clust2 r2 net_b net_se, fmt(%18.0f %18.0f %18.0f %12.2f %12.2f %12.0f %12.0f)) replace  ;


* Table days 16-30 
local opts  "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
mtitles("t+16" "t+17" "t+18" "t+19" "t+20" "t+21" "t+22" "t+23" "t+24" "t+25" "t+26" "t+27" "t+28" "t+29" "t+30"        )
mgroups("Dependent variable: abnormal return\textsubscript{\textbf{s}} in percentage points on day: "  
, pattern(1 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;   

local filename "Ret_sentiment_xsd_16_30_gasvi" ;

esttab i17 i18 i19 i20 i21 i22 i23 i24 i25 i26 i27 i28 i29 i30 i31 using `filename'.tex, `opts' `stats_opts' 
drop(_cons) coef(
1.bull "Declared Bull\textsubscript{s,t}" 
sd_l30 "S.d. received impressions(30days)" 
1.bull#c.sd_l30 "Bull\textsubscript{s,t} $\times$ s.d. rec. impressions " 
cabret_l1to5 "Cum. ab. returns\textsubscript{s,(t-5 to t-1)}"
cabret_l6to30 "Cum. ab. returns\textsubscript{s,(t-30 to t-6)}"
asvi_l5day "Log GoogleASVI\textsubscript{s,(t-5 to t-1)}"
asvi5_abret5 "(Log GoogleASVI $\times$ Cum. ab. ret.)\textsubscript{s,(t-5 to t-1)}" 
) replace  ;

esttab i17 i18 i19 i20 i21 i22 i23 i24 i25 i26 i27 i28 i29 i30 i31 using `filename'.csv, se nostar stats(N N_clust1 N_clust2 r2 net_b net_se, fmt(%18.0f %18.0f %18.0f %12.2f %12.2f %12.0f %12.0f)) replace  ;

#delimit cr 
cd "`=maindir'" 

cap n erase temp_regs_onreturns.dta


